﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
using YouMa.Model;

namespace YouMa.Reposity
{
    public class ReposityBase
    {
        public YouMaContext db;

        public ReposityBase(YouMaContext _db) { db=_db; }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public TEntity GetEntity<TEntity>(Expression<Func<TEntity,bool>> func)where TEntity : class
        {
            return db.Set<TEntity>().Where(func).FirstOrDefault();
        }

        /// <summary>
        /// 根据条件委托，查询实体列表。
        /// </summary>
        public List<TEntity> GetList<TEntity>(Expression<Func<TEntity, bool>> func) where TEntity : class
        {
            return db.Set<TEntity>().Where(func).ToList();
        }

        /// <summary>
        /// 插入实体。
        /// </summary>
        public bool InsertEntity<TEntity>(TEntity entity) where TEntity : class
        {
            db.Set<TEntity>().Add(entity);
            return db.SaveChanges() > 0;
        }
        public TEntity InsertEntity2<TEntity>(TEntity entity) where TEntity : class
        {
            db.Set<TEntity>().Add(entity);
            db.SaveChanges();
            return entity;
        }

        /// <summary>
        /// 根据委托条件，查询是否存在满足条件的记录。
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate">委托条件。</param>
        /// <returns></returns>
        public bool Exists<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return db.Set<TEntity>().Any(predicate);
        }


        public IQueryable<T> QueryEntityQueryable<T>(Expression<Func<T, bool>> predicate) where T : class
        {
            return db.Set<T>().Where(predicate);
        }


        public virtual bool UpdateEntity<T>(T entity) where T : class
        {
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }

        public virtual bool DeleteEntity<TEntity>(TEntity entity) where TEntity : class
        {
            var dbEntityEntry = db.Entry<TEntity>(entity);
            dbEntityEntry.State = EntityState.Deleted;
            db.Set<TEntity>().Remove(entity);
            return db.SaveChanges() > 0;

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual bool DeleteEntityRange<TEntity>(List<TEntity> list) where TEntity : class
        {
            list.ForEach(entity => {
                var dbEntityEntry = db.Entry<TEntity>(entity);
                dbEntityEntry.State = EntityState.Deleted;
                db.Set<TEntity>().Remove(entity);
            });
          
            return db.SaveChanges() > 0;
        }

        public virtual bool AddRange<T>(ICollection<T> range) where T : class
        {
            db.Set<T>().AddRange(range);
            return db.SaveChanges() > 0;
        }


    }
}
